package com.improvess.rna.som.output;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.improvess.rna.som.KNNBasedProbabilistcSOM;

@SuppressWarnings("serial")
public class NewKnnPbSOMDialog extends JDialog {

	private JButton ok;
	private JButton cancel;
	private ShowNet showNet;
	private JTextField textSigmaZero;
	private JComboBox comboNumeroNeuronios;
	private JTextField textK;
	private JTextField textTaxaDecrementoSigma;


	private JTextField textNumeroAtributos;

	public NewKnnPbSOMDialog(ShowNet showNet) {
		super(showNet, true);
		this.showNet = showNet;

		init();
	}

	private void init() {
		Image img = new ImageIcon(this.getClass().getResource("edit24.png"))
				.getImage();
		this.setIconImage(img);

		JLabel title = new JLabel("Set your network:");
		title.setBounds(50, 10, 150, 30);
		this.getContentPane().add(title);

		this.setTitle("New Probabilistic KNN SOM net");
		this.setBounds(350, 200, 300, 420);
		this.setResizable(false);
		this.setLayout(null);

		this.comboNumeroNeuronios = new JComboBox(new Integer[] { 1, 4, 9, 16,
				25, 36, 49, 64, 81 });
		this.comboNumeroNeuronios.setSelectedIndex(2);
		this.comboNumeroNeuronios.setBounds(150, 60, 60, 30);
		JLabel labelNumeroNeuronios = new JLabel("# Neurons:");
		labelNumeroNeuronios.setBounds(25, 60, 100, 30);
		this.getContentPane().add(this.comboNumeroNeuronios);
		this.getContentPane().add(labelNumeroNeuronios);

		this.textK = new JTextField();
		this.textK.setText("9");
		this.textK.setBounds(150, 160, 60, 30);
		JLabel labelK = new JLabel("K:");
		labelK.setBounds(25, 160, 100, 30);
		this.getContentPane().add(this.textK);
		this.getContentPane().add(labelK);

		this.textSigmaZero = new JTextField();
		this.textSigmaZero.setBounds(150, 210, 60, 30);
		this.textSigmaZero.setText("0.6");
		JLabel labelSigmaZero = new JLabel("Sigma Zero:");
		labelSigmaZero.setBounds(25, 210, 100, 30);
		this.getContentPane().add(this.textSigmaZero);
		this.getContentPane().add(labelSigmaZero);

		this.textTaxaDecrementoSigma = new JTextField();
		this.textTaxaDecrementoSigma.setBounds(150, 260, 60, 30);
		this.textTaxaDecrementoSigma.setText("0.005");
		JLabel labelTaxaDecSigma = new JLabel("Dec. sigma:");
		labelTaxaDecSigma.setBounds(25, 260, 100, 30);
		this.getContentPane().add(this.textTaxaDecrementoSigma);
		this.getContentPane().add(labelTaxaDecSigma);

		this.textNumeroAtributos = new JTextField();
		this.textNumeroAtributos.setBounds(150, 310, 60, 30);
		this.textNumeroAtributos.setText("2");
		JLabel labelNumeroAtributos = new JLabel("# Atributos:");
		labelNumeroAtributos.setBounds(25, 310, 100, 30);
		this.getContentPane().add(this.textNumeroAtributos);
		this.getContentPane().add(labelNumeroAtributos);

		cancel = new JButton("Cancel");
		ok = new JButton("Ok!");
		ok.setBounds(100, 360, 80, 30);
		cancel.setBounds(200, 360, 80, 30);

		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				NewKnnPbSOMDialog.this.setVisible(false);
			}

		});

		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (NewKnnPbSOMDialog.this.showNet.getDataSetTreinamento() != null) {
					double sigmaZero = Double.parseDouble(textSigmaZero
							.getText());
					double taxaDecrementoSigmaZero = Double.parseDouble(textTaxaDecrementoSigma
							.getText());
					int quantidadeAtributos = Integer
							.parseInt(textNumeroAtributos.getText());
					int k = Integer
					.parseInt(textK.getText());

					KNNBasedProbabilistcSOM redeSOM = new KNNBasedProbabilistcSOM((Integer) NewKnnPbSOMDialog.this.comboNumeroNeuronios
							.getSelectedItem(), quantidadeAtributos, sigmaZero, NewKnnPbSOMDialog.this.showNet.getDataSetTreinamento()
							.lenght(), k, taxaDecrementoSigmaZero);
						
						
					NewKnnPbSOMDialog.this.showNet.setRedeSOM(redeSOM);
					NewKnnPbSOMDialog.this.setVisible(false);
					NewKnnPbSOMDialog.this.showNet.repaint();
				} else {
					JOptionPane.showMessageDialog(null, "Please set data set first.");
				}
			}

		});

		this.getContentPane().add(ok);
		this.getContentPane().add(cancel);
	}
}
